package com.navinfo.platform.common.utils;

import scala.Tuple2;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.TimeZone;

/**
 * @author gxs
 * 时间转换类
 */
public class DateUtils {

    public static final String DEFAULT_DATETIME_FORMAT = "yyyy-MM-dd HH:mm:ss";

    public static String format(long mills, String pattern){
       return DateFormatThreadLocal.getFormat(pattern).format(mills);
    }

    public static String format(long mills){
        return DateFormatThreadLocal.getFormat().format(mills);
    }

    public static String format(Date date){
        return DateFormatThreadLocal.getFormat().format(date);
    }

    public static String format(Date date, String pattern){
        return DateFormatThreadLocal.getFormat(pattern).format(date);
    }

    public static String format(String dateStr, String originPattern, String targetPattern){
        SimpleDateFormat simpleDateFormat = DateFormatThreadLocal.getFormat(originPattern);
        try {
            Date date = simpleDateFormat.parse(dateStr);
            simpleDateFormat.applyPattern(targetPattern);
            return simpleDateFormat.format(date);
        } catch (ParseException e) {
            return null;
        }
    }
    /**
     * 获取周或月的起始和结束时间
     * @param day  当前日志
     * @param type 0周类型，1月类型
     * @return
     */
    public static Tuple2<String, String> getStartEndDay(String day, String type) {
        String startDay = "";
        String endDay = startDay;
        DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyyMMdd");
        LocalDate inputDate = LocalDate.parse(day, df);

        switch (type) {
            case StatisticsDef.DAY:
                //本周开始时间
                startDay = df.format(inputDate);
                //本周结束时间
                endDay = df.format(inputDate);
                break;
            case StatisticsDef.WEEK:
                //本周开始时间
                startDay = df.format(inputDate.with(DayOfWeek.MONDAY));
                //本周结束时间
                endDay = df.format(inputDate.with(DayOfWeek.SUNDAY));
                break;
            case StatisticsDef.MONTH:
                //本月的第一天
                startDay = df.format(inputDate.withDayOfMonth(1));
                //本月的最后一天
                endDay = df.format(inputDate.withDayOfMonth(inputDate.lengthOfMonth()));
                break;
            case StatisticsDef.YEAR:
                //本月的第一天
                startDay = df.format(inputDate.withDayOfYear(1));
                //本月的最后一天
                endDay = df.format(inputDate.withDayOfYear(inputDate.lengthOfYear()));
                break;
            default:
                break;
        }
        return new Tuple2<>(startDay, endDay);
    }

    private static class DateFormatThreadLocal {
        private static ThreadLocal<SimpleDateFormat> THREAD_SAFE_FORMATTER = new ThreadLocal<SimpleDateFormat>(){
            @Override
            protected synchronized SimpleDateFormat initialValue() {
                SimpleDateFormat sd = new SimpleDateFormat(DEFAULT_DATETIME_FORMAT);
                sd.setTimeZone(TimeZone.getTimeZone("GMT+8"));
                return sd;
            }
        };

        public static SimpleDateFormat getFormat(){
            return THREAD_SAFE_FORMATTER.get();
        }

        public static SimpleDateFormat getFormat(String pattern){
            SimpleDateFormat format = getFormat();
            format.applyPattern(pattern);
            return format;
        }
    }
}
